package lunbo

import (
	"server/global"
	"server/model/common/response"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

var A轮播图api = new(S轮播图api)

type S轮播图api struct {
}

func (a *S轮播图api) A增(c *gin.Context) {
	var info S轮播图model
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = A轮播图service.A增(&info)
	if err != nil {
		global.GVA_LOG.Error("新增失败!", zap.Error(err))
		response.FailWithMessage("新增失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (a *S轮播图api) A删(c *gin.Context) {
	var ids any
	id := c.Query("ID")
	if id != "" {
		ids = id
		// fmt.Printf("删除一个 ids 👉 %+v\n", ids)
	} else {
		ids = c.QueryArray("IDs[]")
		// fmt.Printf("删除多个 ids 👉 %+v\n", ids)
	}
	err := A轮播图service.A删(ids)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (a *S轮播图api) A改(c *gin.Context) {
	var info S轮播图model
	err := c.ShouldBindJSON(&info)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = A轮播图service.A改(info)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

func (a *S轮播图api) A查(c *gin.Context) {
	ID := c.Query("ID")
	if ID != "" {
		relunbo, err := A轮播图service.A查单个(ID)
		if err != nil {
			global.GVA_LOG.Error("查询失败!", zap.Error(err))
			response.FailWithMessage("查询失败:"+err.Error(), c)
			return
		}
		response.OkWithData(relunbo, c)
		return
	}

	var pageInfo S轮播图查询req
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := A轮播图service.A查(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

func (a *S轮播图api) GetA轮播图Public(c *gin.Context) {
	// 此接口不需要鉴权 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	A轮播图service.GetA轮播图Public()
	response.OkWithDetailed(gin.H{"info": "不需要鉴权的轮播图接口信息"}, "获取成功", c)
}
